package admin

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"iris_blog/utils"
)

type Nav struct {
}

func (a Nav) GetNavList(i iris.Context) {

	results := []map[string]interface{}{}
	utils.DB.Raw("SELECT * FROM nav").Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})
}

func (a Nav) AddNav(i iris.Context) {

	result := map[string]interface{}{}

	i.ReadJSON(&result)
	fmt.Println(result)

	results := []map[string]interface{}{}
	utils.DB.Raw("INSERT INTO nav (title, link,add_time) VALUES (?,?,?)", result["Title"], result["Link"], utils.GetUnix()).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})
}

func (a Nav) DelNav(i iris.Context) {

	result := map[string]interface{}{}
	i.ReadJSON(&result)

	results := map[string]interface{}{}
	err := utils.DB.Raw("DELETE FROM nav WHERE id = ? and isDelete != 1", result["id"]).Find(&results).Error
	if err != nil {
		i.JSON(map[string]interface{}{
			"code": 20001,
			"data": "删除错误",
		})
	}
	
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})
}

func (a Nav) EditNav(i iris.Context) {

	result := map[string]interface{}{}

	i.ReadJSON(&result)
	fmt.Println(result)

	results := []map[string]interface{}{}
	utils.DB.Raw("UPDATE nav SET  title=?, link=?  WHERE id=?",result["title"],result["link"],result["id"]).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})
}
